package com.example.tool;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.fasterxml.jackson.annotation.JsonFormat;

/**
 * @author 袁小弘
 * @describe 日期工具类
 * @date 2019-12-30 11:00
 */
public class DateTool {

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private static Timestamp recordTime;

    /**
     * @return 返回一个Timestamp时间
     * @describe 获取当前时间转为Timestamp格式
     */
    public static Timestamp getTimestamp() {
        // 记录时间戳
        // 1.
        /*
         * SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String
         * time=df.format(new Date()); Timestamp timeStamp=Timestamp.valueOf(time);
         */

        // 2.
        Timestamp timeStamp = new Timestamp(System.currentTimeMillis());

        return timeStamp;
    }

    public static Timestamp transferTime(String time) {

//		Date date1 = null;
        DateFormat df2 = null;
        Timestamp timeStamp = null;
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            Date date = df.parse(time);
//			SimpleDateFormat df1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
//			date1 = df1.parse(date.toString());
            df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            timeStamp = Timestamp.valueOf(df2.format(date));
        } catch (ParseException e) {

            e.printStackTrace();
        }
        return timeStamp;

    }

    public static String setTimeString(Timestamp ts) {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(ts);

        return time;
    }

}
